/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.caigourenyuan;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.canting.R;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.defineview.ClearEditText;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.immersionbar.ImmersionBar;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.Mobile;
import com.mingmen.canting.util.SharePreferenceUtil;
import com.mingmen.canting.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class AddAddressActivity extends Activity {

    @BindView(R.id.text_return)
    TextView textReturn;
    @BindView(R.id.linear_return)
    LinearLayout linearReturn;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.mToolbar)
    Toolbar mToolbar;
    @BindView(R.id.btn_add_address)
    Button btnRegister;
    @BindView(R.id.lable_selectcanting)
    LinearLayout lableSelectcanting;
    @BindView(R.id.edit_address)
    ClearEditText editAddress;
    @BindView(R.id.houseNumber)
    ClearEditText houseNumber;
    @BindView(R.id.linkman)
    ClearEditText linkman;
    @BindView(R.id.sex_select)
    CheckBox sexSelect;
    @BindView(R.id.sex_mael)
    CheckBox sexMael;
    @BindView(R.id.linkTel)
    ClearEditText linkTel;

    @BindView(R.id.layout_all)
    RelativeLayout layoutAll;

    String token;

    int flag = 0;
    String sex;
    String supplierId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        ButterKnife.bind(this);
        textCenter.setText("新增地址");
        EventBus.getDefault().register(this);
        ImmersionBar.with(this).titleBar(mToolbar)
                .navigationBarColor(R.color.black).init();
        token = (String) SharePreferenceUtil.get(this, "token", "");
        supplierId = getIntent().getStringExtra("supplierId");
        sexSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    flag = 1;
                    sex = "男";
                    sexMael.setChecked(false);
                } else {
                    flag = 0;
                    sex = "";
                }
            }
        });
        sexMael.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    flag = 1;
                    sex = "女";
                    sexSelect.setChecked(false);
                } else {
                    flag = 0;
                    sex = "";
                }
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @OnClick({R.id.text_return, R.id.linear_return, R.id.btn_add_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_return:
                finish();
                break;
            case R.id.linear_return:
                finish();
                break;
            case R.id.btn_register:
                break;
            case R.id.btn_add_address:
                if (editAddress.getText().toString().isEmpty()) {
                    Toast.makeText(this, "请填写收货地址", Toast.LENGTH_SHORT).show();
                } else if (houseNumber.getText().toString().isEmpty()) {
                    Toast.makeText(this, "请填写收货门牌号", Toast.LENGTH_SHORT).show();
                } else if (linkman.getText().toString().isEmpty()) {
                    Toast.makeText(this, "请填写收货人姓名", Toast.LENGTH_SHORT).show();
                } else if (linkTel.getText().toString().isEmpty()) {
                    Toast.makeText(this, "请填写收货人手机号", Toast.LENGTH_SHORT).show();
                } else if (!Mobile.isMobile(linkTel.getText().toString())) {
                    ToastUtil.showToast(this, "请输入正确手机号");
                } else if (flag == 0) {
                    Toast.makeText(this, "请选择性别", Toast.LENGTH_SHORT).show();
                } else {
                    HttpResponse.addAddress(token, editAddress.getText().toString(), "1", linkman.getText().toString(), linkTel.getText().toString(), houseNumber.getText().toString(), supplierId, sex, new StringCallback(this) {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            Log.d("ADDADDREss", s);
                            BaseBean baseBean = new BaseBean();
                            baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                            if (baseBean.getCode()==0){
                                EventBus.getDefault().post(40);
                                finish();
                            }
                            Toast.makeText(AddAddressActivity.this, baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                        }
                    });
                }


                break;
        }
    }

    public void onEventMainThread(Integer integer) {


    }
}